package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class IndexController {

  @RequestMapping("/login")
  public String scanLogin(HttpServletRequest request) throws Exception{
    HashMap jsonpObject = new HashMap<String,String>();
    //appid:企业的corpID
    jsonpObject.put("appid","xxxxxxxxxxx");
    //企业自建应用的应用id
    jsonpObject.put("agentid","xxxx");
    //扫码成功后转向的页面地址
    jsonpObject.put("redirect_uri","xxxx");
    //将此对象传入需要扫码登录的页面中
    request.setAttribute("jsapi_config",jsonpObject);
    return "scanlogin";
  }

  @RequestMapping("/index")
  public String testIndex(HttpServletRequest request) throws Exception{
    return "index";
  }

  @RequestMapping("/login_success")
  public String loginSuccess(String code,HttpServletRequest request, HttpServletResponse response) throws Exception{
    //使用code获取用户身份API：https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo
    System.out.println(code);
    //判断用户身份是否为管理员
    if(true){
      //是管理员身份可进行正常登录到主页,并存入session，下次免登陆
      HttpSession session = request.getSession();
      session.setAttribute("isLogin","yes");
    }else{
      //请使用管理员身份登录
      return "/login";
    }
    return "index";
  }
}
